﻿' Copyright (c) Microsoft Corporation.  All rights reserved.
Imports EdmMetadataHelpersVB
Imports System.IO

Partial Class EditModel
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ShowModel(Request.QueryString("Model"))
        End If
    End Sub
    Protected Sub ShowModel(ByVal modelName As String)
        If (modelName Is Nothing) Or (modelName = "[Add a model]") Then
            txtModel_Name.Text = "[New Model]"
            txtModel_Provider.Text = EdmWrapper.DEFAULT_PROVIDER
            txtModel_ProviderConnectionString.Text = EdmWrapper.DEFAULT_PROVIDER_CONN_STR
        Else
            Dim edm = EdmWrappers.Current.FindByName(modelName)
            If edm Is Nothing Then
                Throw New ArgumentNullException(String.Format("Couldn't find an Edm called: {0}", modelName))
            End If
            txtModel_Name.Text = edm.Name
            txtModel_Provider.Text = edm.Provider
            txtModel_ProviderConnectionString.Text = edm.ProviderConnectionString
        End If
    End Sub
    Protected Sub btnModelSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnModelSave.Click
        Dim name = txtModel_Name.Text.Trim()
        Dim provider = txtModel_Provider.Text.Trim()
        Dim providerConnectionstring = txtModel_ProviderConnectionString.Text.Trim()

        Dim container = EdmWrappers.Current.FindByName(name)

        'We can't do anything if we are in create model (i.e. no container) and we don't have all the files
        If (container Is Nothing) And (Not fileModel_Csdl.HasFile Or Not fileModel_Ssdl.HasFile Or Not fileModel_Msl.HasFile) Then
            Return 'Later we should show some message (i.e. make a warning label visible)
        End If

        Dim newCsdl = container Is Nothing OrElse fileModel_Csdl.HasFile
        Dim newSsdl = container Is Nothing OrElse fileModel_Ssdl.HasFile
        Dim newMsl = container Is Nothing OrElse fileModel_Msl.HasFile

        Dim csdlPath As String = If(newCsdl, GetTempFilePath("csdl"), container.CsdlPath)
        Dim ssdlPath As String = If(newSsdl, GetTempFilePath("ssdl"), container.SsdlPath)
        Dim mslPath As String = If(newMsl, GetTempFilePath("msl"), container.MslPath)

        If newCsdl Then
            StoreFile(fileModel_Csdl, csdlPath)
        End If
        If newSsdl Then
            StoreFile(fileModel_Ssdl, ssdlPath)
        End If
        If newMsl Then
            StoreFile(fileModel_Msl, mslPath)
        End If

        'remove the existing container
        If container IsNot Nothing Then
            EdmWrappers.Current.Remove(container)
        End If
        'add the new container
        container = New EdmWrapper(name, provider, providerConnectionstring, csdlPath, ssdlPath, mslPath)

        EdmWrappers.Current.Insert(0, container)

        Response.Redirect("Default.aspx", True)
    End Sub

    Protected Sub btnModelCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnModelCancel.Click
        Response.Redirect("Default.aspx", True)
    End Sub
    Protected Function GetTempFilePath(ByVal extension As String) As String
        Return Path.GetTempFileName().Replace(".tmp", "." + extension)
    End Function
    Protected Sub StoreFile(ByVal control As FileUpload, ByVal path As String)
        Dim reader As New StreamReader(control.FileContent)
        Dim writer As New StreamWriter(File.OpenWrite(path))
        writer.Write(reader.ReadToEnd())
        writer.Close()
        reader.Close()
    End Sub

End Class
